////////////////////////////////////////////////////////////////////////////////
//                                                                            //                                                                           //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRayPickupFactory extends NavigationPoint
             placeable;

var	transient PrimitiveComponent	PickupMesh;
var ParticleSystem ParticleEffectsEmitter;
var ParticleSystem ParticleEffectsGlow;
var ParticleSystemComponent PickupEffect;
var ParticleSystemComponent BaseGlow;
var		SoundCue			PickupSound;

var float RespawnEffectTime;

var vector BaseGlowOffset;
var rotator MeshRotation;

var bool bTouched;
var bool bPickupHidden;
var repnotify class<Inventory>				InventoryType;

var vector StartLoc;

simulated event PreBeginPlay()
{
	SetPickupMesh();
    bPickupHidden = false;
    BaseGlow = WorldInfo.MyEmitterPool.SpawnEmitter(ParticleEffectsGlow, Location + BaseGlowOffset);
    BaseGlow.SetScale(2.2);
    BaseGlow.SetActive(true);
	super.PreBeginPlay();
}

simulated function SetPickupMesh()
{
	if ( InventoryType.Default.PickupFactoryMesh != None )
	{
		if (PickupMesh != None)
		{
			DetachComponent(PickupMesh);
			PickupMesh = None;
		}
		PickupMesh = new(self) InventoryType.default.PickupFactoryMesh.class(InventoryType.default.PickupFactoryMesh);

		AttachComponent(PickupMesh);

	}
}

function Reset()
{
	GotoState('Pickup');
	Super.Reset();
}

function SpawnCopyFor( Pawn Recipient )
{
	local Inventory Inv;
	if(!bTouched)
	{
        PickupEffect = WorldInfo.MyEmitterPool.SpawnEmitter(ParticleEffectsEmitter, Location);
        PickupEffect.SetScale(0.1);
    }

    SetTimer(1, false, 'DestroyEffect');
    Recipient.PlaySound( PickupSound );
	Recipient.MakeNoise(0.2);
	Inv = spawn(InventoryType);
	if ( Inv != None )
	{
		Inv.GiveTo(Recipient);
	}
}

function bool ReadyToPickup(float MaxWait)
{
	return true;
}

function GiveTo( Pawn P )
{
	SpawnCopyFor(P);
	PickedUpBy(P);
}

function PickedUpBy(Pawn P)
{
	TriggerEventClass(class'SeqEvent_PickupStatusChange', P, 1);

	if (P.Controller != None && P.Controller.MoveTarget == self)
	{
		P.SetAnchor(self);
		P.Controller.MoveTimer = -1.0;
	}
}

auto state Pickup
{
    function bool ReadyToPickup(float MaxWait)
	{
		return true;
	}

	function bool ValidTouch( Pawn Other )
	{
        if(Other.IsHumanControlled())
            return true;
        return false;

	}

	event Touch( Actor Other, PrimitiveComponent OtherComp, vector HitLocation, vector HitNormal )
	{
		local Pawn P;

		// If touched by a player pawn, let him pick this up.
		P = Pawn(Other);

		if( P != None && ValidTouch(P) )
		{
			GiveTo(P);
			SetPickupHidden();
 		}

	}
	// Make sure no pawn already touching (while touch was disabled in sleep).
	function CheckTouching()
	{
		local Pawn P;

		ForEach TouchingActors(class'Pawn', P)
			Touch(P, None, Location, Normal(Location-P.Location) );
	}

	event BeginState(name PreviousStateName)
	{
		TriggerEventClass(class'SeqEvent_PickupStatusChange', None, 0);
	}

Begin:
	CheckTouching();
}

State Disabled
{
	function bool ReadyToPickup(float MaxWait)
	{
		return false;
	}

	function Reset() {}
	function StartSleeping() {}

	simulated event SetInitialState()
	{
		bScriptInitialized = true;
	}

	simulated event BeginState(Name PreviousStateName)
	{
		SetHidden(true);
		SetCollision(false,false);

	}
}

simulated function SetPickupVisible()
{
    bPickupHidden = false;
    SetCollision(true,true);
    SetHidden(false);
    BaseGlow.SetActive(true);
}

simulated function SetPickupHidden()
{
	bPickupHidden = true;
	SetHidden(true);
	SetCollision(false,false);
	BaseGlow.SetActive(false);

}


simulated function Respawn(Pawn P)
{
    if(P == none)
    {
            if(bPickupHidden)
            {
                 SetPickupVisible();
                 GotoState('Pickup');
             }

    }
}

function Tick(float DeltaTime)
{
    local BailterRayPlayerController P;

    if(!bTouched)
        StartLoc = self.Location;

    if(!bPickupHidden)
    {
       RespawnEffectTime += DeltaTime;
       if(RespawnEffectTime >= 3)
       {
           BaseGlow.DeactivateSystem();
           BaseGlow = WorldInfo.MyEmitterPool.SpawnEmitter(ParticleEffectsGlow, Location + BaseGlowOffset);
           BaseGlow.SetScale(2.2);
           BaseGlow.SetActive(true);
           RespawnEffectTime = 0;
       }
    }

    foreach LocalPlayerControllers(class'BailterRayPlayerController',P)
    {
         if(P.IsPlayerOwned())
             break;
    }
    Respawn(P.Pawn);
    UpdateRotation(DeltaTime);
}

simulated function UpdateRotation(float DeltaTime)
{
    MeshRotation.Yaw += 32768 * DeltaTime;
    PickupMesh.SetRotation(MeshRotation);
}

simulated function DestroyEffect()
{
     PickupEffect.SetActive(false);
}

defaultproperties
{
   bTouched=false
   Begin Object Name=CollisionCylinder ObjName=CollisionCylinder
      CollisionHeight=80.000000
      CollisionRadius=40.000000
      CollideActors=True
   End Object
   BaseGlowOffset = (X=0 ,Y=0, Z=0)
   Begin Object Class=DynamicLightEnvironmentComponent Name=PickupLightEnvironment ObjName=PickupLightEnvironment Archetype=DynamicLightEnvironmentComponent'UTGame.Default__UTPickupFactory:PickupLightEnvironment'
      ObjectArchetype=DynamicLightEnvironmentComponent'UTGame.Default__UTPickupFactory:PickupLightEnvironment'
   End Object
   Components(5)=PickupLightEnvironment
   CylinderComponent=CollisionCylinder
   Components(3)=CollisionCylinder
   bStatic=False
   bAlwaysRelevant=True
   bCollideWhenPlacing=False
   bCollideActors=True
   CollisionComponent=CollisionCylinder
   CollisionType=COLLIDE_TouchAll
   SupportedEvents(3)=Class'Engine.SeqEvent_PickupStatusChange'
   InventoryType=class'UTGame.UTPickupInventory'
   Name="Default__BailterRayPickupFactory"
   ObjectArchetype=NavigationPoint'Engine.Default__NavigationPoint'
